package com.example.textview.util;

import android.util.Log;



import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.security.InvalidParameterException;

public class FxTCP extends Tcpclass {
    //    Socket
    private Socket socket;
    //    IP地址
    private String ipAddress;
    //    端口号
    private int port;
    //    Socket输出流
    private OutputStream outputStream;
    //    Socket输入流
    private InputStream inputStream;


    private void waitForMills(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    private String fixAdress(String M) {
        // M=M.toUpperCase();
        String index = M.substring(1);
        String strHex = Integer.toHexString(Integer.parseInt(index));
        strHex = strHex.toUpperCase();
        StringBuilder sb = new StringBuilder(strHex);//构造一个StringBuilder对象
        for (int i = sb.length(); i < 8; i++) {
            sb.insert(0, "0");
        }
        strHex = sb.toString();


        sb = new StringBuilder();//构造一个StringBuilder对象
        for (int k = 0; k < 4; k++) {

            sb.insert(0, strHex.substring(k * 2, k * 2 + 2));
        }
        strHex = sb.toString();

        return strHex;
    }


    public String writeBool(String M, String Status) {

        String ID = "02FF0000";
        M = M.toUpperCase();
        String strHex = fixAdress(M);


        if (M.contains("M")) {
            ID = ID + strHex + "204D" + "0100";
        } else if (M.contains("Y")) {
            ID = ID + strHex + "2059" + "0100";

        }


        StringBuilder sb = new StringBuilder(Status);//构造一个StringBuilder对象

        for (int i = Status.length(); i < 2; i++) {

            sb.insert(1, "0");
        }

        ID = ID + sb.toString();

        byte[] bytes = hexStringToByteArray(ID);
        this.send(bytes);
        Log.d("hex", ID);


       // this.receive();

        return ID;

    }


    public String readBool(String M, String count_hex) {

        String reciveStr=null;
        String ID = "00FF0A00";
        M = M.toUpperCase();
        String adrHex = fixAdress(M);

        if (M.contains("M")) {
            ID = ID + adrHex + "204D";
        } else if (M.contains("Y")) {
            ID = ID + adrHex + "2059";

        } else if (M.contains("X")) {
            ID = ID + adrHex + "2058";

        }

        if (count_hex.length() < 2)
            count_hex += "0";


        ID = ID + count_hex + "00";


        byte[] bytes = hexStringToByteArray(ID);
        this.send(bytes);

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {

            return null;
        }

        this.receive();

        if(reciveStr!=null)
        {
            if(reciveStr.length()>4)
            {
                reciveStr= reciveStr.substring(4);
            }
        }


        return reciveStr;


    }

    public String readWord(String adr,int count){

        String count_str= HexDump.toHexString((byte) count);
        String reciveStr=null;
        String ID = "01FF0A00";

        adr = adr.toUpperCase();
        String adrHex = fixAdress(adr);

        ID = ID + adrHex + "2044"+ count_str+"00";

        byte[] bytes = hexStringToByteArray(ID);
        this.send(bytes);

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {

            return null;
        }

        // this.receive();

        if(reciveStr!=null)
        {
            if(reciveStr.length()>4)
            {
                reciveStr= reciveStr.substring(4);
            }
        }


        return reciveStr;


    }

    public void writeWord(String adr,short data){

        String data_str=HexDump.toHexString(data);
        String ID = "03FF0000";
        adr = adr.toUpperCase();
        String strHex = fixAdress(adr);

        ID = ID + strHex + "2044" + "0100"+data_str;

        byte[] bytes = hexStringToByteArray(ID);
        this.send(bytes);

         this.receive();
    }

    public void writeFloat(String adr,float value)
    {
        String valueHex=  HexDump.float2HexString(value);
        String ID = "03FF0000";
        adr = adr.toUpperCase();
        String strHex = fixAdress(adr);

        ID = ID + strHex + "2044" + "0200"+valueHex;

        byte[] bytes = hexStringToByteArray(ID);
        this.send(bytes);

        this.receive();

    }


    public String readFloat(String adr){

        String return_Value=null;
        String result=null;
        return_Value=this.readWord(adr, (byte) 2);
        if(return_Value!=null)
            if(return_Value.length()==8) {
                return_Value = return_Value.substring(4) + return_Value.substring(0, 4);

                double f = HexDump.hexString2Float(return_Value);

                result = String.valueOf(f);
            }
        return result;

    }




    private  int toByte(char c) {
        if (c >= '0' && c <= '9')
            return (c - '0');
        if (c >= 'A' && c <= 'F')
            return (c - 'A' + 10);
        if (c >= 'a' && c <= 'f')
            return (c - 'a' + 10);

        throw new InvalidParameterException("Invalid hex char '" + c + "'");
    }

    public  byte[] hexStringToByteArray(String hexString) {
        int length = hexString.length();
        byte[] buffer = new byte[length / 2];

        for (int i = 0; i < length; i += 2) {
            buffer[i / 2] = (byte) ((toByte(hexString.charAt(i)) << 4) | toByte(hexString
                    .charAt(i + 1)));
        }

        return buffer;
    }


    public void setConnectedCallback(Tcpclass.OnServerConnectedCallbackBlock connectedCallback) {
        this.connectedCallback = connectedCallback;
    }

}

